<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/12
 * Time: 15:29
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Feedback extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_username|uuu.user_phone|feedback_content|feedback_phone'] = ['like',"%" . $keyword . "%"];
            }

            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.feedback_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.feedback_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.feedback_time'] = ['between time',[$start,$end]];
            }
            if(isset($status)&&$status!="")
            {
                $map['feedback_status'] = $status;
            }
            $count = Db::table('up_feedback')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.f_user_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_feedback')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.f_user_id','left')
                ->where($map)
                ->field('uuu.user_username,uuu.user_phone,a.*')
                ->order('feedback_status asc,feedback_time desc')
                ->page($page, $limit)
                ->select();//计算总页面

            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    
    /**
     * 意见反馈详情
     * @return mixed
     */
    public function handle(){
        if(Request::instance()->isPost()){
            $id = input('fid');
            $status = trim(input('status'), ' ');
            $content = trim(input('content'), ' ');
            $data = [
                'feedback_handle_content' => $content
            ];
            if($status){
                $data['feedback_status'] = $status;
            }
            $res = Db::table('up_feedback')->where('feedback_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $feedback_id = input('feedback_id');
        // banner
        $banner = Db::table('up_feedback')
            ->where('feedback_id', $feedback_id)
            ->find();
        $this->assign([
            'res' => $banner
        ]);
        return $this->fetch('feedback/detail');
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $car_id = input('id');
        try{
            // 删除车辆
            Db::table('up_feedback')->where('feedback_id', $car_id)->delete();
            // 删除类别
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}